fig.path <- "results/graph/"
tab.path <- "results/table/"
tab.name <- "pap_tab_02.tex"

Indcontr <- select(Re, all_of(c(controls, "G_UNIT"))) %>%
    distinct()  %>%
    rename(g_unit = G_UNIT)

Ind$gender<- na_if(Ind$gender, "na")
Ind$female <- (Ind$gender == "female")

### Table 
Labs <- c("Total Electors", "Local Electors", "Number of Women", 
 "Share Local over Total Electors" , "Share Women in Local Electors")

Ind2 <- Ind %>% 
    left_join(Indcontr, by = "g_unit")  %>%
    group_by(g_name, g_unit, year, type ) %>%
    add_count( name = "tot_type") %>%
    ungroup() %>%
    group_by(g_name, g_unit, year, treat_gp )  %>%
    add_count( name = "tot_tot") %>% 
    mutate( share_type =   tot_type/tot_tot, 
    share_type_100 =   tot_type/tot_tot*100
    ) %>%
    group_by(g_name, g_unit, year, treat_gp, type ) %>%
    mutate(tot_women_type = sum(female, na.rm = TRUE)) %>% 
    mutate(share_wm_typ = tot_women_type/tot_type) %>%
    group_by(g_name, g_unit, year, treat_gp ) %>%
    mutate(tot_wm = sum(female, na.rm = TRUE) ) %>%
    mutate(share_wm = tot_wm/tot_tot) 


Ind3 <- subset(Ind2, type == "occupation division 3"  |  type == "ownership division 4")

Ind4 <- Ind3 %>% ungroup() %>% 
    subset(type == "occupation division 3") %>%
    select(all_of(c("share_wm_typ", "share_type", "tot_wm", "share_wm", "year", "g_unit", "treat_gp")) ) %>%
    distinct() %>%
    arrange(g_unit, year) %>%
    group_by(g_unit, treat_gp ) %>%
    mutate(diff_ge = share_wm - lag(share_wm) ,  
    diff_sh =  share_type - lag(share_type) ,
    g_sh_100 =  (share_type - lag(share_type))/lag(share_type)*100 ,
    g_sh_wm_100 = (share_wm - lag(share_wm))/lag(share_wm)*100,
    g_wm_100 = (tot_wm - lag(tot_wm))/lag(tot_wm)*100)  %>%
    left_join(Indcontr)


Ind5 <- Ind2 %>% ungroup() %>%
    subset( type == "occupation division 3")   %>%
    select(any_of(c("g_unit", "year", "share_type_100"))) %>% 
    distinct() %>%
    rename(share_local = share_type_100 ) %>%
    right_join(Ind2, by = c("g_unit", "year")) 

Ind6 <- Ind5 %>% ungroup() %>%
    subset( type == "occupation division 3" & !is.na(female)) %>%
    mutate(klus = paste0(year, "-",treat_gp)) #Note that one parish has 0 division 3 electors in 1914 so drops out of sample (Nidd)


vars1 <-  c( "factor(year)*factor(treat_gp)","factor(year)" ,"factor(treat_gp)")
vars2 <-  c( "g_sh_100")
vars3 <- c("share_local", "factor(year)")

outvars1 <- c( "factor.year.1914.factor.treat_gp.1",
    "^factor.treat_gp.1$", "^factor.year.1914$")
labvars1 <- c("DPost X March",  "March", "DPost")

outvars2 <- c( "g_sh_100")
labvars2 <- c("Share Local Electors, Growth")

outvars3 <- c( "share_local")
labvars3 <- c("Share Local Electors")

outvars <- c(outvars2, outvars1, outvars3)
labvars <- c(labvars2, labvars1, labvars3)

Basse <- c( "log.pop" )
Kont <- c( "log.min.dist.city10k", 
    "single_" , "illeg_rate", "f_smam", "hc3", "hc1", 
    "hc5", "fmar_pr" , "m_cel_4", "ecmr", "fwid_pr", "sexr", "hc2")

depvar <- c( "Growth Fem Electors", "Share Fem Elector" , 
    "Share Female Elector",  "1.Female Elector", "1.Female Elector" )

re1 <- felm(as.formula(RegFor( y = "g_wm_100" , x = c(vars2 ) ,
                              FE = "0" , IV="0", clust = "0" )),
           data= Ind4)

re2 <- felm(as.formula(RegFor( y = "female" , x = c(vars1) ,
                              FE = "0" , IV="0", clust = "g_unit" )),
           data= Ind6 ) 

re3 <- felm(as.formula(RegFor( y = "female" , x = c(vars1,  Basse, Kont, "log(tot_tot)") ,
                              FE = "0" , IV="0", clust = "g_unit" )),
           data= Ind6 )

regstoadd <- list(re1, re2,re3)

MSd <- function(variable){
  x1 <- round(mean(variable, na.rm = TRUE), 2)
  x2<-  round(sd(variable, na.rm = TRUE), 2)
  return(c(x1,x2))
}

Stats <- rbind(MSd(Ind4$g_wm_100),  MSd(Ind6$female), MSd(Ind6$female))

####OUT
add.lines <- list(AddLines( 6, "Data Aggregation", c(LMc("District"),
        LMc("Individual"), LMc("Individual"))),
        AddLines( 6, "County ", c(LMc("WR of Yorkshire"),
            LMc("WR of Yorkshire"), LMc("WR of Yorkshire"))),
        AddLines( 6, "Years", c( LMc("1914"), LMc("1911 \\& 14"), LMc("1911 \\& 14"))),
        AddLines( 6, "Controls", c( LMc("No"), LMc("No"), LMc("Yes"))),
        AddLines( 6, "Mean dep. var." , Stats[,1] ),
        AddLines( 6, "Sd dep. var." , Stats[,2] )
                 )

fileConn<-file(paste0(tab.path,tab.name))
print(fileConn)

writeLines(stargazer(regstoadd,
                     float = FALSE ,
                     keep= outvars,
                     order = outvars ,
                     covariate.labels= labvars ,
                     star.cutoffs = c(0.1099, 0.0599, 0.0199) ,
                     digits.extra = 0,
                     multicolumn=F,#
                     column.labels = c("Fem Voter (grow)", "Fem Voter (share)"),
                     column.separate = c(1,2),
                    #  column.labels = NULL,
                     dep.var.labels.include = F,
                     font.size = "footnotesize",
                     omit.table.layout = "n",
                     align =T,
                     add.lines = add.lines,
                     digits = 3,
                     intercept.top = T,
                     intercept.bottom = F,
                     keep.stat = c("rsq" , "n") ,
                     omit.stat = c("res.dev","ser") ),
           fileConn)
close(fileConn)

print(summary(re3))
rm(list=
    setdiff(ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))],
    c("Re","Ind")))